/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdMiningChallenge;

import com.SyDeck.sdCities.CityInfo;
import com.SySammy.SdCurrency.sdCCommandExport;
import com.SySammy.sdMiningChallenge.prop.MiningProperties;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

class sdCMiningCmds implements CommandExecutor {

    private final sdMC plugin;
    private challengeMethods CM = new challengeMethods();
    private static sdCCommandExport CC = new sdCCommandExport();

    public sdCMiningCmds(sdMC aThis) {
        this.plugin = aThis;
    }

    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        String[] Nargs = strings;
        String cmd = cmnd.getName().toLowerCase();
        if (cmd.equals("mining")) {
            return MiningMethod(cs, Nargs);
        }
        return true;
    }

    private boolean MiningMethod(CommandSender cs, String[] Nargs) {
        String[] args = Nargs;
        Player ply = (Player) cs;
        if (args.length <= 0) {
            if (!plugin.casUseMiningInfo(ply)) {
                ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            if (challengeMethods.started == false && challengeMethods.joining == true) {
                ply.sendMessage(ChatColor.DARK_AQUA + "Challenge currently open to join");
                ply.sendMessage(ChatColor.DARK_AQUA + "Entry fee: " + ChatColor.GOLD + challengeMethods.entryfee);
                ply.sendMessage(ChatColor.DARK_AQUA + "Prize: " + ChatColor.GOLD + challengeMethods.prize);
                return true;
            }
            if (challengeMethods.started == true) {
                ply.sendMessage(ChatColor.DARK_AQUA + "Challenge currently running, stats:");
                ply.sendMessage(ChatColor.DARK_AQUA + "Scores: " + ChatColor.GOLD + challengeMethods.scores.toString());
                ply.sendMessage(ChatColor.DARK_AQUA + "Time Left: " + ChatColor.GOLD + CM.timeLeft() + " sec.");
                return true;
            } else {
                ply.sendMessage(ChatColor.RED + "No challenge started at the moment.");
                return true;
            }
        } else if (args[0].equalsIgnoreCase("-l") && args.length >= 1) {
            try {
                if (!plugin.casUseMiningList(ply)) {
                    ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                String[] ores = MiningProperties.ores.split(";");
                ply.sendMessage(ChatColor.DARK_AQUA + "Usable Materials:");
                for (int x = 0; x < ores.length; x++) {
                    ply.sendMessage(ChatColor.GOLD + ores[x]);
                }
                return true;
            } catch (Exception e) {
            }
        } else if (args[0].equalsIgnoreCase("-s") && args.length >= 6) {
            try {
                if (!plugin.casUseMiningSet(ply)) {
                    ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                CityInfo ci = new CityInfo();
                String citycheck = ci.CityNameByBlock(ply.getTargetBlock(null, 5).getLocation().getBlockX(), ply.getTargetBlock(null, 5).getLocation().getBlockZ());
                if (citycheck != null) {
                    ply.sendMessage(ChatColor.RED + "The Challange can't be made inside a city.");
                    return true;
                }
                if (!CM.UsableOre(args[1].toUpperCase())) {
                    ply.sendMessage(ChatColor.RED + "That material isn't permitted.");
                    return true;
                }
                if (!CC.HasMoney(ply.getName(), MiningProperties.challengeCost + Integer.parseInt(args[4]))) {
                    ply.sendMessage(ChatColor.RED + "You don't have enough money: " + ChatColor.GOLD + MiningProperties.challengeCost + " + " + Integer.parseInt(args[4]) + " " + CC.GetCurrency());
                    return true;
                }
                if ((CM.time(args[1])[1] * Integer.parseInt(args[2])) > MiningProperties.MaxTime) {
                    ply.sendMessage(ChatColor.RED + "The max quantity for " + args[1].toUpperCase() + " is: " + (MiningProperties.MaxTime / CM.time(args[1])[1]));

                    return true;
                }
                CM.SetChallenge(ply.getName(), plugin, ply.getTargetBlock(null, 5).getLocation(), args[1], Integer.parseInt(args[2]), Integer.parseInt(args[3]), Integer.parseInt(args[4]));
                ply.sendMessage(ChatColor.DARK_AQUA + "The challenge will start in " + ChatColor.GOLD + MiningProperties.WaitingTime + " min.");
                ply.sendMessage(ChatColor.DARK_AQUA + "Minimum players: " + ChatColor.GOLD + MiningProperties.MinPlayers);
                ply.getServer().broadcastMessage(ChatColor.DARK_AQUA + "[MC]: A mining challenge will start in " + ChatColor.GOLD + MiningProperties.WaitingTime + " min.");
                ply.getServer().broadcastMessage(ChatColor.DARK_AQUA + "Tip: /mining -j - to teleport and take part on the challenge");
                ply.getServer().broadcastMessage(ChatColor.DARK_AQUA + "Entry fee: " + ChatColor.GOLD + challengeMethods.entryfee);
                ply.getServer().broadcastMessage(ChatColor.DARK_AQUA + "Prize: " + ChatColor.GOLD + challengeMethods.prize);
                CC.TakeMoney(ply.getName(), Integer.parseInt(args[4]) + MiningProperties.challengeCost);
                return true;
            } catch (Exception e) {
                ply.sendMessage(ChatColor.RED + "input/server error please try again.");
            }
        } else if (args[0].equalsIgnoreCase("-j") && args.length >= 1) {
            try {
                if (!plugin.casUseMiningJoin(ply)) {
                    ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                if (challengeMethods.joining == false) {
                    ply.sendMessage(ChatColor.RED + "Sorry, joining is closed for now.");
                    return true;
                }
                if (challengeMethods.scores.containsKey(ply.getName())) {
                    ply.sendMessage(ChatColor.RED + "You already joined the challenge");
                    return true;
                }
                if (challengeMethods.maker.equalsIgnoreCase(ply.getName())) {
                    ply.sendMessage(ChatColor.RED + "Makers can't join the challenge.");
                    return true;
                }
                if (CC.HasMoney(ply.getName(), challengeMethods.entryfee)) {
                    CM.JoinChallenge(ply);
                } else {
                    ply.sendMessage(ChatColor.RED + "You don't have enough money to join.");
                }
                return true;
            } catch (Exception e) {
                ply.sendMessage(ChatColor.RED + "input/server error please try again.");
            }
        } else if (args[0].equalsIgnoreCase("-h") && args.length >= 1) {
            try {
                if (!plugin.casUseMiningHelp(ply)) {
                    ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                ply.sendMessage(ChatColor.DARK_AQUA + "Making a challenge costs: " + ChatColor.GOLD + MiningProperties.challengeCost + " " + CC.GetCurrency());
                return false;
            } catch (Exception e) {
                ply.sendMessage(ChatColor.RED + "WTF BUMMMMMMMM");
            }
        }
        return true;
    }
}
